﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;

namespace Chenxin.Lib.Passport.DAL
{
    /// <summary>
    /// 数据访问类Hr_UserInfo。
    /// </summary>
    public class Hr_UserInfo
    {
        public Hr_UserInfo()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(Guid UserId)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Hr_UserInfo where UserId=@UserId ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "UserId", DbType.Guid, UserId);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.Passport.Entity.Hr_UserInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Hr_UserInfo(");
            strSql.Append("UserId,UserCode,CityId,CityName,CompanyId,CompanyName,Validate,Password,UserType,UserName,Sex,Education,IDType,IDNo,BirthDay,Address,Mobile,HomeTel,OfficeTel,BankNo,ConBegin,ConEnd,DepartId,DepartName,Position,EmpLevel,ComeDate,FundCode,PassUpdateTime,Remark,AddTime,UpdateTime,AddPerson,AddDepart,Status,NeedChgPass, SerialCode, PicUrl, WorkAgeBegin, BelongCityId,BelongCityName, BelongCompanyId,BelongCompanyName, EMail, BelongDepartId, BelongDepart,Initial,IsVirtual,Zip,PicUrlSmall,WorkRelation)");

            strSql.Append(" values (");
            strSql.Append("@UserId,@UserCode,@CityId,@CityName,@CompanyId,@CompanyName,@Validate,@Password,@UserType,@UserName,@Sex,@Education,@IDType,@IDNo,@BirthDay,@Address,@Mobile,@HomeTel,@OfficeTel,@BankNo,@ConBegin,@ConEnd,@DepartId,@DepartName,@Position,@EmpLevel,@ComeDate,@FundCode,@PassUpdateTime,@Remark,getDate(),getDate(),@AddPerson,@AddDepart,@Status,@NeedChgPass, @SerialCode, @PicUrl,@WorkAgeBegin, @CityId,@CityName,@CompanyId,@CompanyName,@Email,@DepartId,@DepartName,@Initial,@IsVirtual,@Zip,@PicUrlSmall,@WorkRelation)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "UserId", DbType.Guid, model.UserId);
            db.AddInParameter(dbCommand, "UserCode", DbType.AnsiString, model.UserCode);
            db.AddInParameter(dbCommand, "CityId", DbType.Guid, model.CityId);
            db.AddInParameter(dbCommand, "CityName", DbType.AnsiString, model.CityName);
            db.AddInParameter(dbCommand, "CompanyId", DbType.Guid, model.CompanyId);
            db.AddInParameter(dbCommand, "CompanyName", DbType.AnsiString, model.CompanyName);
            db.AddInParameter(dbCommand, "Validate", DbType.Boolean, model.Validate);
            db.AddInParameter(dbCommand, "Password", DbType.AnsiString, model.Password);
            db.AddInParameter(dbCommand, "UserType", DbType.Int32, model.UserType);
            db.AddInParameter(dbCommand, "UserName", DbType.AnsiString, model.UserName);
            db.AddInParameter(dbCommand, "Sex", DbType.Int32, model.Sex);
            db.AddInParameter(dbCommand, "Education", DbType.AnsiString, model.Education);
            db.AddInParameter(dbCommand, "IDType", DbType.AnsiString, model.IDType);
            db.AddInParameter(dbCommand, "IDNo", DbType.AnsiString, model.IDNo);
            db.AddInParameter(dbCommand, "BirthDay", DbType.DateTime, model.BirthDay);
            db.AddInParameter(dbCommand, "Address", DbType.AnsiString, model.Address);
            db.AddInParameter(dbCommand, "Mobile", DbType.AnsiString, model.Mobile);
            db.AddInParameter(dbCommand, "HomeTel", DbType.AnsiString, model.HomeTel);
            db.AddInParameter(dbCommand, "OfficeTel", DbType.AnsiString, model.OfficeTel);
            db.AddInParameter(dbCommand, "BankNo", DbType.AnsiString, model.BankNo);
            db.AddInParameter(dbCommand, "ConBegin", DbType.DateTime, model.ConBegin);
            db.AddInParameter(dbCommand, "ConEnd", DbType.DateTime, model.ConEnd);
            db.AddInParameter(dbCommand, "DepartId", DbType.Guid, model.DepartId);
            db.AddInParameter(dbCommand, "DepartName", DbType.AnsiString, model.DepartName);
            db.AddInParameter(dbCommand, "Position", DbType.AnsiString, model.Position);
            db.AddInParameter(dbCommand, "EmpLevel", DbType.AnsiString, model.EmpLevel);
            db.AddInParameter(dbCommand, "ComeDate", DbType.DateTime, model.ComeDate);
            db.AddInParameter(dbCommand, "FundCode", DbType.AnsiString, model.FundCode);
            db.AddInParameter(dbCommand, "PassUpdateTime", DbType.DateTime, model.PassUpdateTime);
            db.AddInParameter(dbCommand, "Remark", DbType.AnsiString, model.Remark);
            //db.AddInParameter(dbCommand, "AddTime", DbType.DateTime, model.AddTime);
            //db.AddInParameter(dbCommand, "UpdateTime", DbType.DateTime, model.UpdateTime);
            db.AddInParameter(dbCommand, "AddPerson", DbType.AnsiString, model.AddPerson);
            db.AddInParameter(dbCommand, "AddDepart", DbType.AnsiString, model.AddDepart);
            db.AddInParameter(dbCommand, "Status", DbType.Int32, model.Status);
            db.AddInParameter(dbCommand, "NeedChgPass", DbType.Boolean, model.NeedChgPass);

            //add by kimi 2010-3-26
            db.AddInParameter(dbCommand, "SerialCode", DbType.AnsiString, model.SerialCode);
            db.AddInParameter(dbCommand, "PicUrl", DbType.AnsiString, model.PicUrl);
            db.AddInParameter(dbCommand, "WorkAgeBegin", DbType.DateTime, model.WorkAgeBegin);
            db.AddInParameter(dbCommand, "Email", DbType.AnsiString, model.Email);

            // Add by hejifeng 2010-03-30
            db.AddInParameter(dbCommand, "Initial", DbType.AnsiString, model.Initial);
            // Add by tianno 2010-04-07
            db.AddInParameter(dbCommand, "IsVirtual", DbType.Boolean, model.IsVirtual);
            //Add By tianno 2010-7-21
            db.AddInParameter(dbCommand, "Zip", DbType.AnsiString, model.Zip);
            db.AddInParameter(dbCommand, "PicUrlSmall", DbType.AnsiString, model.PicUrlSmall);
            db.AddInParameter(dbCommand, "WorkRelation", DbType.AnsiString, model.WorkRelation);            
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.Passport.Entity.Hr_UserInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Hr_UserInfo set ");
            strSql.Append("UserCode=@UserCode,");
            strSql.Append("CityId=@CityId,");
            strSql.Append("CityName=@CityName,");
            strSql.Append("CompanyId=@CompanyId,");
            strSql.Append("CompanyName=@CompanyName,");
            //add by kimi 2010-3-26
            strSql.Append("BelongCityId=@BelongCityId,");
            strSql.Append("BelongCityName=@BelongCityName,");
            strSql.Append("BelongCompanyId=@BelongCompanyId,");
            strSql.Append("BelongCompanyName=@BelongCompanyName,");
            strSql.Append("Email=@Email,");
            strSql.Append("BelongDepartId=@BelongDepartId,");
            strSql.Append("BelongDepart=@BelongDepart,");

            strSql.Append("Validate=@Validate,");
            //strSql.Append("Password=@Password,");
            strSql.Append("UserType=@UserType,");
            strSql.Append("UserName=@UserName,");
            strSql.Append("Sex=@Sex,");
            strSql.Append("Education=@Education,");
            strSql.Append("IDType=@IDType,");
            strSql.Append("IDNo=@IDNo,");
            strSql.Append("BirthDay=@BirthDay,");
            strSql.Append("Address=@Address,");
            strSql.Append("Mobile=@Mobile,");
            strSql.Append("HomeTel=@HomeTel,");
            strSql.Append("OfficeTel=@OfficeTel,");
            strSql.Append("BankNo=@BankNo,");
            strSql.Append("ConBegin=@ConBegin,");
            strSql.Append("ConEnd=@ConEnd,");
            strSql.Append("DepartId=@DepartId,");
            strSql.Append("DepartName=@DepartName,");
            strSql.Append("Position=@Position,");
            strSql.Append("EmpLevel=@EmpLevel,");
            strSql.Append("ComeDate=@ComeDate,");
            strSql.Append("FundCode=@FundCode,");
            strSql.Append("PassUpdateTime=@PassUpdateTime,");
            strSql.Append("Remark=@Remark,");
            strSql.Append("AddTime=@AddTime,");
            strSql.Append("leavedate=@leavedate,");
            strSql.Append("UpdateTime=getDate(),");
            strSql.Append("AddPerson=@AddPerson,");
            strSql.Append("AddDepart=@AddDepart,");
            strSql.Append("Status=@Status,");
            strSql.Append("SerialCode=@SerialCode,");
            strSql.Append("PicUrl=@PicUrl,");
            strSql.Append("WorkAgeBegin=@WorkAgeBegin,");
            strSql.Append("NeedChgPass=@NeedChgPass,");
            //Add By hejifeng 20100330
            strSql.Append("IsVirtual=@IsVirtual,");
            //Add by tianno 2010-04-07
            strSql.Append("Initial=@Initial,");
            //Add by tianno 2010-07-21
            strSql.Append("Zip=@Zip,");
            strSql.Append("PicUrlSmall=@PicUrlSmall,");
            strSql.Append("WorkRelation=@WorkRelation");
            strSql.Append(" where UserId=@UserId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "UserId", DbType.Guid, model.UserId);
            db.AddInParameter(dbCommand, "UserCode", DbType.AnsiString, model.UserCode);
            db.AddInParameter(dbCommand, "CityId", DbType.Guid, model.CityId);
            db.AddInParameter(dbCommand, "CityName", DbType.AnsiString, model.CityName);
            db.AddInParameter(dbCommand, "CompanyId", DbType.Guid, model.CompanyId);
            db.AddInParameter(dbCommand, "CompanyName", DbType.AnsiString, model.CompanyName);
            db.AddInParameter(dbCommand, "Validate", DbType.Boolean, model.Validate);
            db.AddInParameter(dbCommand, "UserType", DbType.Int32, model.UserType);
            db.AddInParameter(dbCommand, "UserName", DbType.AnsiString, model.UserName);
            db.AddInParameter(dbCommand, "Sex", DbType.Int32, model.Sex);
            db.AddInParameter(dbCommand, "Education", DbType.AnsiString, model.Education);
            db.AddInParameter(dbCommand, "IDType", DbType.AnsiString, model.IDType);
            db.AddInParameter(dbCommand, "IDNo", DbType.AnsiString, model.IDNo);
            db.AddInParameter(dbCommand, "BirthDay", DbType.DateTime, model.BirthDay);
            db.AddInParameter(dbCommand, "Address", DbType.AnsiString, model.Address);
            db.AddInParameter(dbCommand, "Mobile", DbType.AnsiString, model.Mobile);
            db.AddInParameter(dbCommand, "HomeTel", DbType.AnsiString, model.HomeTel);
            db.AddInParameter(dbCommand, "OfficeTel", DbType.AnsiString, model.OfficeTel);
            db.AddInParameter(dbCommand, "BankNo", DbType.AnsiString, model.BankNo);
            db.AddInParameter(dbCommand, "ConBegin", DbType.DateTime, model.ConBegin);
            db.AddInParameter(dbCommand, "ConEnd", DbType.DateTime, model.ConEnd);
            db.AddInParameter(dbCommand, "DepartId", DbType.Guid, model.DepartId);
            db.AddInParameter(dbCommand, "DepartName", DbType.AnsiString, model.DepartName);
            db.AddInParameter(dbCommand, "Position", DbType.AnsiString, model.Position);
            db.AddInParameter(dbCommand, "EmpLevel", DbType.AnsiString, model.EmpLevel);
            db.AddInParameter(dbCommand, "ComeDate", DbType.DateTime, model.ComeDate);
            db.AddInParameter(dbCommand, "FundCode", DbType.AnsiString, model.FundCode);
            db.AddInParameter(dbCommand, "PassUpdateTime", DbType.DateTime, model.PassUpdateTime);
            db.AddInParameter(dbCommand, "Remark", DbType.AnsiString, model.Remark);
            db.AddInParameter(dbCommand, "AddTime", DbType.DateTime, model.AddTime);
            db.AddInParameter(dbCommand, "leavedate", DbType.DateTime, model.LeaveDate);
            db.AddInParameter(dbCommand, "AddPerson", DbType.AnsiString, model.AddPerson);
            db.AddInParameter(dbCommand, "AddDepart", DbType.AnsiString, model.AddDepart);
            db.AddInParameter(dbCommand, "Status", DbType.Int32, model.Status);
            db.AddInParameter(dbCommand, "NeedChgPass", DbType.Boolean, model.NeedChgPass);

            //add by kimi 2010-3-26
            db.AddInParameter(dbCommand, "SerialCode", DbType.AnsiString, model.SerialCode);
            db.AddInParameter(dbCommand, "PicUrl", DbType.AnsiString, model.PicUrl);
            db.AddInParameter(dbCommand, "WorkAgeBegin", DbType.DateTime, model.WorkAgeBegin);
            db.AddInParameter(dbCommand, "BelongCityId", DbType.Guid, model.BelongCityId);
            db.AddInParameter(dbCommand, "BelongCityName", DbType.AnsiString, model.BelongCityName);
            db.AddInParameter(dbCommand, "BelongCompanyId", DbType.Guid, model.BelongCompanyId);
            db.AddInParameter(dbCommand, "BelongCompanyName", DbType.AnsiString, model.BelongCompanyName);
            db.AddInParameter(dbCommand, "BelongDepartId", DbType.Guid, model.BelongDepartId);
            db.AddInParameter(dbCommand, "BelongDepart", DbType.AnsiString, model.BelongDepart);
            db.AddInParameter(dbCommand, "Email", DbType.AnsiString, model.Email);
            //Add By hejifeng 20100330
            db.AddInParameter(dbCommand, "Initial", DbType.AnsiString, model.Initial);
            //Add By tianno 2010-04-07
            db.AddInParameter(dbCommand, "IsVirtual", DbType.Boolean, model.IsVirtual);
            //Add By tianno 2010-7-21
            db.AddInParameter(dbCommand, "Zip", DbType.AnsiString, model.Zip);
            db.AddInParameter(dbCommand, "PicUrlSmall", DbType.AnsiString, model.PicUrlSmall);
            db.AddInParameter(dbCommand, "WorkRelation", DbType.AnsiString, model.WorkRelation);  
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid UserId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Hr_UserInfo ");
            strSql.Append(" where UserId=@UserId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "UserId", DbType.Guid, UserId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 通过UserId得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.Passport.Entity.Hr_UserInfo GetEntityById(Guid UserId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select UserId,UserCode,CityId,CityName,CompanyId,CompanyName,Validate,Password,UserType,UserName,Sex,Education,IDType,IDNo,BirthDay,Address,Mobile,HomeTel,OfficeTel,BankNo,ConBegin,ConEnd,DepartId,DepartName,Position,EmpLevel,ComeDate,FundCode,PassUpdateTime,Remark,AddTime,UpdateTime,AddPerson,AddDepart,Status,NeedChgPass,LeaveDate, PicUrl,SerialCode, WorkAgeBegin,BelongCityId,BelongCityName,BelongCompanyId,BelongCompanyName, Email, BelongDepartId, BelongDepart,Initial,IsVirtual,Zip,PicUrlSmall,WorkRelation from Hr_UserInfo ");
            strSql.Append(" where UserId=@UserId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "UserId", DbType.Guid, UserId);
            Chenxin.Lib.Passport.Entity.Hr_UserInfo model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 通过UserCode得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.Passport.Entity.Hr_UserInfo GetEntityByCode(string UserCode)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select UserId,UserCode,CityId,CityName,CompanyId,CompanyName,Validate,Password,UserType,UserName,Sex,Education,IDType,IDNo,BirthDay,Address,Mobile,HomeTel,OfficeTel,BankNo,ConBegin,ConEnd,DepartId,DepartName,Position,EmpLevel,ComeDate,FundCode,PassUpdateTime,Remark,AddTime,UpdateTime,AddPerson,AddDepart,Status,NeedChgPass,LeaveDate, PicUrl,SerialCode,WorkAgeBegin,BelongCityId,BelongCityName,BelongCompanyId,BelongCompanyName, Email, BelongDepartId, BelongDepart,Initial,IsVirtual,Zip,PicUrlSmall,WorkRelation  from Hr_UserInfo ");
            strSql.Append(" where UserCode=@UserCode ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "UserCode", DbType.String, UserCode);
            Chenxin.Lib.Passport.Entity.Hr_UserInfo model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 通过UserCode得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.Passport.Entity.Hr_UserInfo GetEntityById(string UserId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select UserId,UserCode,CityId,CityName,CompanyId,CompanyName,Validate,Password,UserType,UserName,Sex,Education,IDType,IDNo,BirthDay,Address,Mobile,HomeTel,OfficeTel,BankNo,ConBegin,ConEnd,DepartId,DepartName,Position,EmpLevel,ComeDate,FundCode,PassUpdateTime,Remark,AddTime,UpdateTime,AddPerson,AddDepart,Status,NeedChgPass,LeaveDate, PicUrl,SerialCode,WorkAgeBegin,BelongCityId,BelongCityName,BelongCompanyId,BelongCompanyName, Email, BelongDepartId, BelongDepart,Initial,IsVirtual,Zip,PicUrlSmall,WorkRelation  from Hr_UserInfo ");
            strSql.Append(" where UserId=@UserId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "UserId", DbType.String, UserId);
            Chenxin.Lib.Passport.Entity.Hr_UserInfo model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }


        //直接执行SQL语句
        public static bool ExecuteSQL(string strSQL)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            int count = db.ExecuteNonQuery(CommandType.Text, strSQL);
            if (count > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 获得数据列表(不分页)
        /// </summary>
        public static DataSet ExecuteSql(string strSql)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql);
        }

        //返回执行SQL语句的总数
        public static bool CountBySQL(string strSql)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            int count = (int)db.ExecuteScalar(CommandType.Text, strSql);
            if (count > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strSelect,string strWhere,string strOrder,int intCurPage, int intRowCnt)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTable");
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, intCurPage);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, intRowCnt);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, strOrder);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, "Hr_UserInfo");
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, strWhere);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, strSelect);
            //db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, TableFieldList);
            //db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, DoCount);
            return db.ExecuteDataSet(dbCommand);
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageIndex, int PageSize, String OrderField, string TableName, string StrWhere, string FieldList, string TableFieldList, bool DoCount)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTable");
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, OrderField);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, TableName);
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, StrWhere);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, FieldList);
            db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, TableFieldList);
            db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, DoCount);
            return db.ExecuteDataSet(dbCommand);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select UserId,UserCode,CityId,CityName,CompanyId,CompanyName,Validate,Password,UserType,UserName,Sex,Education,IDType,IDNo,BirthDay,Address,Mobile,HomeTel,OfficeTel,BankNo,ConBegin,ConEnd,DepartId,DepartName,Position,EmpLevel,ComeDate,FundCode,PassUpdateTime,Remark,AddTime,UpdateTime,AddPerson,AddDepart,Status,NeedChgPass,LeaveDate, PicUrl,SerialCode,WorkAgeBegin,BelongCityId,BelongCityName,BelongCompanyId,BelongCompanyName, Email,BelongDepartId, BelongDepart,Initial,IsVirtual,Zip,PicUrlSmall,WorkRelation  ");
            strSql.Append(" FROM Hr_UserInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("UP_GetRecordByPage");
            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "Hr_UserInfo");
            db.AddInParameter(dbCommand, "fldName", DbType.AnsiString, "ID");
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "IsReCount", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "OrderType", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
            return db.ExecuteDataSet(dbCommand);
        }*/
        
        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.Passport.Entity.Hr_UserInfo> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select UserId,UserCode,CityId,CityName,CompanyId,CompanyName,Validate,Password,UserType,UserName,Sex,Education,IDType,IDNo,BirthDay,Address,Mobile,HomeTel,OfficeTel,BankNo,ConBegin,ConEnd,DepartId,DepartName,Position,EmpLevel,ComeDate,FundCode,PassUpdateTime,Remark,AddTime,UpdateTime,AddPerson,AddDepart,Status,NeedChgPass,LeaveDate, PicUrl,SerialCode,WorkAgeBegin,BelongCityId,BelongCityName,BelongCompanyId,BelongCompanyName, Email,BelongDepartId, BelongDepart ,Initial,IsVirtual,Zip,PicUrlSmall,WorkRelation ");
            strSql.Append(" FROM Hr_UserInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.Passport.Entity.Hr_UserInfo> list = new List<Chenxin.Lib.Passport.Entity.Hr_UserInfo>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        public static List<Chenxin.Lib.Passport.Entity.Hr_UserInfo> GetListArray(string strWhere, string strOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select UserId,UserCode,CityId,CityName,CompanyId,CompanyName,Validate,Password,UserType,UserName,Sex,Education,IDType,IDNo,BirthDay,Address,Mobile,HomeTel,OfficeTel,BankNo,ConBegin,ConEnd,DepartId,DepartName,Position,EmpLevel,ComeDate,FundCode,PassUpdateTime,Remark,AddTime,UpdateTime,AddPerson,AddDepart,Status,NeedChgPass,LeaveDate, PicUrl,SerialCode,WorkAgeBegin,BelongCityId,BelongCityName,BelongCompanyId,BelongCompanyName, Email,BelongDepartId, BelongDepart ,Initial,IsVirtual,Zip,PicUrlSmall,WorkRelation ");
            strSql.Append(" FROM Hr_UserInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }

            if (strOrder.Trim() != "")
            {
                strSql.Append(" Order By " + strOrder);
            }

            List<Chenxin.Lib.Passport.Entity.Hr_UserInfo> list = new List<Chenxin.Lib.Passport.Entity.Hr_UserInfo>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }

        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.Passport.Entity.Hr_UserInfo ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.Passport.Entity.Hr_UserInfo model = new Chenxin.Lib.Passport.Entity.Hr_UserInfo();
            object ojb;
            model.UserCode = dataReader["UserCode"].ToString();
            ojb = dataReader["UserId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.UserId = new Guid(ojb.ToString());
            }
            ojb = dataReader["CityId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CityId = new Guid(ojb.ToString());
            }
            model.CityName = dataReader["CityName"].ToString();
            ojb = dataReader["CompanyId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CompanyId = new Guid(ojb.ToString());
            }
            model.CompanyName = dataReader["CompanyName"].ToString();
            ojb = dataReader["Validate"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Validate = (bool)ojb;
            }
            model.Password = dataReader["Password"].ToString();
            ojb = dataReader["UserType"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.UserType = (int)ojb;
            }
            model.UserName = dataReader["UserName"].ToString();
            ojb = dataReader["Sex"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Sex = (int)ojb;
            }
            model.Education = dataReader["Education"].ToString();
            model.IDType = dataReader["IDType"].ToString();
            model.IDNo = dataReader["IDNo"].ToString();
            ojb = dataReader["BirthDay"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.BirthDay = (DateTime)ojb;
            }
            model.Address = dataReader["Address"].ToString();
            model.Mobile = dataReader["Mobile"].ToString();
            model.HomeTel = dataReader["HomeTel"].ToString();
            model.OfficeTel = dataReader["OfficeTel"].ToString();
            model.BankNo = dataReader["BankNo"].ToString();
            ojb = dataReader["ConBegin"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ConBegin = (DateTime)ojb;
            }
            ojb = dataReader["ConEnd"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ConEnd = (DateTime)ojb;
            }
            ojb = dataReader["DepartId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DepartId = new Guid(ojb.ToString());
            }
            model.DepartName = dataReader["DepartName"].ToString();
            ojb = dataReader["Position"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Position = ojb.ToString();
            }
            ojb = dataReader["EmpLevel"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.EmpLevel = ojb.ToString();
            }
            ojb = dataReader["ComeDate"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ComeDate = (DateTime)ojb;
            }
            model.FundCode = dataReader["FundCode"].ToString();
            ojb = dataReader["PassUpdateTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.PassUpdateTime = (DateTime)ojb;
            }
            model.Remark = dataReader["Remark"].ToString();
            ojb = dataReader["AddTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AddTime = (DateTime)ojb;
            }
            ojb = dataReader["UpdateTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.UpdateTime = (DateTime)ojb;
            }
            model.AddPerson = dataReader["AddPerson"].ToString();
            model.AddDepart = dataReader["AddDepart"].ToString();
            ojb = dataReader["Status"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Status = (int)ojb;
            }
            ojb = dataReader["NeedChgPass"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.NeedChgPass = (bool)ojb;
            }
            ojb = dataReader["LeaveDate"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.LeaveDate = (DateTime)ojb;
            }
            //add by kimi 2010-3-26
            model.PicUrl = dataReader["PicUrl"].ToString();
            model.SerialCode = dataReader["SerialCode"].ToString();
            ojb = dataReader["WorkAgeBegin"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.WorkAgeBegin = (DateTime)ojb;
            }

            ojb = dataReader["BelongCityId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.BelongCityId = new Guid(ojb.ToString());
            }
            model.BelongCityName = dataReader["BelongCityName"].ToString();
            ojb = dataReader["BelongCompanyId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.BelongCompanyId = new Guid(ojb.ToString());
            }
            model.BelongCompanyName = dataReader["BelongCompanyName"].ToString();

            ojb = dataReader["BelongDepartId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.BelongDepartId = new Guid(ojb.ToString());
            }
            model.BelongDepart = dataReader["BelongDepart"].ToString();

            model.Email = dataReader["Email"].ToString();
            //Add By hejifeng 20100330
            model.Initial = dataReader["Initial"].ToString();

            //Add By tianno 2010-04-07
            ojb = dataReader["IsVirtual"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.IsVirtual = (bool)ojb;
            }
            //Add By tianno 2010-07-21
            model.Zip = dataReader["Zip"].ToString();
            model.PicUrlSmall = dataReader["PicUrlSmall"].ToString();
            model.WorkRelation = dataReader["WorkRelation"].ToString();            
            return model;
        }

        public static int getTotalCnt(string strWhere)
        {
            int intTotalCnt=0;

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) as cnt");
            strSql.Append(" FROM Hr_UserInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }

            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    intTotalCnt = (int)dataReader["cnt"];
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return intTotalCnt;
        }

        public static void resetPassword(Guid gdUserId,string newpwd)
        {
            StringBuilder strSql = new StringBuilder();
            string strPsw = "";

            System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();//实例化一个md5对像
            // 加密后是一个字节类型的数组，这里要注意编码UTF8/Unicode等的选择　
            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(newpwd));
            // 通过使用循环，将字节类型的数组转换为字符串，此字符串是常规字符格式化所得
            for (int i = 0; i < s.Length; i++)
            {
                // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母，如果使用大写（X）则格式后的字符是大写字符

                strPsw = strPsw + s[i].ToString("X").PadLeft(2,'0');
            }

            strSql.Append("update Hr_UserInfo set ");
            strSql.Append("Password=@Password,PassUpdateTime=getdate(),NeedChgPass=1 ");
            strSql.Append(" where UserId=@UserId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "UserId", DbType.Guid, gdUserId);
            db.AddInParameter(dbCommand, "Password", DbType.AnsiString, strPsw);
            db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="model"></param>
        public static void ChangePassword(Chenxin.Lib.Passport.Entity.Hr_UserInfo model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update Hr_UserInfo set ");
            strSql.Append("NeedChgPass=@NeedChgPass,");
            strSql.Append("PassUpdateTime=@PassUpdateTime,");
            strSql.Append("Password=@Password ");
            strSql.Append(" where UserId=@UserId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "UserId", DbType.Guid, model.UserId);
            db.AddInParameter(dbCommand, "Password", DbType.AnsiString, model.Password);
            db.AddInParameter(dbCommand, "NeedChgPass", DbType.AnsiString, model.NeedChgPass);
            db.AddInParameter(dbCommand, "PassUpdateTime", DbType.AnsiString, model.PassUpdateTime);
            db.ExecuteNonQuery(dbCommand);
        }

        #endregion  成员方法
    }
}

